package tech.yunland.shop.web;

import com.moyuer.bsf.common.Pagination;
import com.moyuer.bsf.domain.model.User;
import com.moyuer.bsf.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.yunland.shop.domain.model.Dormitories;
import tech.yunland.shop.domain.service.DormitoriesService;
import tech.yunland.shop.domain.service.OrganDormitoriesService;
import tech.yunland.shop.domain.service.UserDormitoriesService;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by v on 17-7-3.
 */
@RestController
@RequestMapping("/dormitories")
public class DormitoriesController extends BaseController {

    @Autowired
    private DormitoriesService dormitoriesService;

    @Autowired
    private UserDormitoriesService userDormitoriesService;

    @Autowired
    private OrganDormitoriesService organDormitoriesService;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Pagination<Dormitories> get(@RequestParam(required = false) String param,
                                       @RequestParam(required = false) Integer page,
                                       @RequestParam(required = false) Integer rows) {
        Pagination result = new Pagination(page, rows);
        Integer startIdx = result.getStartIdx();
        Integer count = dormitoriesService.getSize(param, this.organIds);
        List<Map<String, Object>> records = dormitoriesService.get(param, startIdx, rows, this.organIds);
        result.setCount(count);
        result.setRecords(records);
        return result;
    }

    /**
     * 新增寝室
     *
     * @param dormitories
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(@RequestBody @Valid Dormitories dormitories) {
        dormitoriesService.save(dormitories, primaryOrgan);
    }

    /**
     * 给班级分配寝室
     */
    @RequestMapping(value = "/set", method = RequestMethod.POST)
    public void set(@RequestParam(required = false) String odid,
                    @RequestParam(required = false) String oid) {
        organDormitoriesService.set(odid, oid);
    }


    @RequestMapping(value = "/getStu", method = RequestMethod.GET)
    public Pagination<User> getStu(@RequestParam(required = false) String did,
                                   @RequestParam(required = false) Integer page,
                                   @RequestParam(required = false) Integer rows) {
        Pagination result = new Pagination(page, rows);
        Integer startIdx = result.getStartIdx();
        Integer count = userDormitoriesService.getStuSize(did);
        List<Map<String, Object>> records = userDormitoriesService.get(did, startIdx, rows);
        result.setCount(count);
        result.setRecords(records);
        return result;
    }
    /**
     * 给学生安排寝室
     *
     * @param did
     * @param uids
     */
    @RequestMapping(value = "/choice", method = RequestMethod.POST)
    public void choice(@RequestParam(required = false) String did,
                       @RequestParam(required = false) List<String> uids) {
        userDormitoriesService.choice(did, uids);
    }


}
